﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class RoomPanel : BasePanel
{
    private Transform UserInfo;
    private Transform RoomInfo;
    private Text UserName;
    private Text TotalLabel;
    private Text WinLabel;
    private VerticalLayoutGroup UiGrid;
    [SerializeField]
    private RoomSlot RoomSlot;
    private Button CloseBtn;
    private Button CreateBtn;
    private List<UserData> userList = null;
    private RoomRequest roomRequest;

    private void Start()
    {
        UserInfo = transform.Find("UserInfo");
        UserName = UserInfo.Find("UserName").GetComponent<Text>();
        TotalLabel = UserInfo.Find("TotalLabel").GetComponent<Text>();
        WinLabel = UserInfo.Find("WinLabel").GetComponent<Text>();
        RoomInfo = transform.Find("RoomInfo");
        UiGrid = RoomInfo.Find("ScrollView/Grid").GetComponent<VerticalLayoutGroup>();
        //RoomSlot = UiGrid.content.Find("RoomSlot").GetComponent<RoomSlot>();
        CloseBtn = RoomInfo.Find("CloseBtn").GetComponent<Button>();
        CloseBtn.onClick.AddListener(OnClose);
        CreateBtn = transform.Find("RoomInfo/CreateBtn").GetComponent<Button>();
        CreateBtn.onClick.AddListener(OnCreateRoom);
        roomRequest = GetComponent<RoomRequest>();

        UserName.text = GameFacade.Instance.UserData.UserName;
        TotalLabel.text = string.Format("总场数:{0}", GameFacade.Instance.UserData.Total);
        WinLabel.text = string.Format("胜场数:{0}", GameFacade.Instance.UserData.Win);
    }

    public override void OnEnter()
    {
        base.OnEnter();
        //gameObject.SetActive(true);
        if(roomRequest == null)
            roomRequest = GetComponent<RoomRequest>();
        roomRequest.SendRequest();
    }

    private void OnClose()
    {
        uiMgr.PopPanel();
    }

    public override void OnPause()
    {
        base.OnPause();
        //gameObject.SetActive(false);
    }

    public override void OnExit()
    {
        base.OnExit();
        //gameObject.SetActive(false);
    }

    public override void OnResume()
    {
        base.OnResume();
        roomRequest.SendRequest();
    }

    private void Update()
    {
        if (userList != null)
        {
            LoadRoomItem(userList);
            userList = null;
        }
    }

    public void LoadRoomItemSync(List<UserData> udList)
    {
        userList = udList;
    }

    private void LoadRoomItem(List<UserData> udList)
    {
        RoomSlot[] slots = UiGrid.GetComponentsInChildren<RoomSlot>();
        foreach (RoomSlot slot in slots)
        {
            slot.DestroySelf();
        }

        for (int i = 0; i < udList.Count; i++)
        {
            GameObject prefab = Instantiate(RoomSlot.gameObject) as GameObject;
            prefab.transform.SetParent(UiGrid.transform);
            prefab.transform.localPosition = Vector3.zero;
            prefab.transform.localScale = Vector3.one;
            RoomSlot slot = prefab.GetComponent<RoomSlot>();
            if (slot != null)
                slot.SetData(udList[i].UserName, udList[i].Total, udList[i].Win);
        }
        UiGrid.GetComponent<RectTransform>().sizeDelta = new Vector2(0f, RoomSlot.GetComponent<RectTransform>().sizeDelta.y * udList.Count + UiGrid.GetComponent<VerticalLayoutGroup>().spacing * (udList.Count - 1));
    }

    private void OnCreateRoom()
    {
        uiMgr.PushPanel(UIPanelType.Match);
    }
}
